using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class LoadABStreamingAssets : MonoBehaviour
{
    private GameObject targetGameObject = null;
    private AssetBundle targetAB = null;
    public string abStreamingAssetPath = null;

    private void DestroyGameObject()
    {
        abStreamingAssetPath = Application.streamingAssetsPath + "/oh/loadabpath";
        Debug.Log(abStreamingAssetPath);
        if (targetGameObject != null)
        {
            GameObject.Destroy(targetGameObject);
            targetGameObject = null;
        }

        if (targetAB != null)
        {
            targetAB.Unload(true);
            targetAB = null;
        }
        
    }

    private void OnGUI()
    {
        if (GUI.Button(new Rect(300, 300, 100, 100), "AB Api Load"))
        {
            DestroyGameObject();
            targetAB = AssetBundle.LoadFromFile(abStreamingAssetPath);
            if (targetAB == null)
            {
                Debug.Log("[LoadABStreamingAssets]Load from file failed!");
                return;
            }

            Debug.Log("[LoadABStreamingAssets]Load AB Success");
            GameObject asset = targetAB.LoadAsset<GameObject>("Cube");
            if (asset == null)
            {
                Debug.Log("[LoadABStreamingAssets]Get Cube asset failed!");
                return;
            }

            targetGameObject = GameObject.Instantiate(asset);
        }

        if (GUI.Button(new Rect(300, 500, 100, 100), "UnityWebRequst Api Load"))
        {
            DestroyGameObject();
            StartCoroutine(UnityWebRequestLoadAB());
        }

        if (GUI.Button(new Rect(300, 700, 100, 100), "WWW Api Load"))
        {
            DestroyGameObject();
            StartCoroutine(WWWLoadAB());
        }
    }

    IEnumerator UnityWebRequestLoadAB()
    {
#if UNITY_EDITOR
        UnityWebRequest unityWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(" file://" + abStreamingAssetPath);
#else
        UnityWebRequest unityWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(abStreamingAssetPath);
#endif
        yield return unityWebRequest.SendWebRequest();
        Debug.Log(unityWebRequest.result);
        targetAB = DownloadHandlerAssetBundle.GetContent(unityWebRequest);
        if (targetAB != null)
        {
            GameObject asset = targetAB.LoadAsset<GameObject>("Cube");
            if (asset == null)
            {
                Debug.Log("[LoadABStreamingAssets]Get Cube asset failed!");
            }
            else
            {
                targetGameObject = GameObject.Instantiate(asset);
            }
        }
        else
        {
            Debug.Log("[LoadABStreamingAssets]UnityWebRequestLoadAB failed!" + abStreamingAssetPath);
        }
    }

    IEnumerator WWWLoadAB()
    {
#if UNITY_EDITOR
        WWW www = new WWW(" file://" + abStreamingAssetPath);
#else
        WWW www = new WWW(abStreamingAssetPath);
#endif
        yield return www;
        AssetBundle ab = www.assetBundle;
        if (ab != null)
        {
            GameObject asset = ab.LoadAsset<GameObject>("Cube");
            if (asset == null)
            {
                Debug.Log("[LoadABStreamingAssets]Get Cube asset failed!");
            }
            else
            {
                targetGameObject = GameObject.Instantiate(asset);
            }
        }
        else
        {
            Debug.Log("[LoadABStreamingAssets]WWWLoadAB failed!" + abStreamingAssetPath);
        }
    }
}